<?php

 
class vkapi {
	var $api_secret;
	var $app_id;
	var $api_url;
	var $code;
        var $token;
	function vkapi($app_id, $api_secret, $redirect_url) {
		$this->app_id = $app_id;
		$this->api_secret = $api_secret;
                $this->redirect_to = $redirect_url;
	}
	
        function getToken($code) {
        $query = 'https://oauth.vk.com/access_token?client_id=' . $this->app_id . '&client_secret=' . $this->api_secret . '&' .
                'code=' . $code . '&' .
                'redirect_uri=' . $this->redirect_to;

        if($result = @file_get_contents($query))
        {
            return json_decode($result, true);
        }
        return FALSE;
    }

    function api($method, $token, $params = false) {
        if (!$params)
            $params = array();

        $query = "https://api.vk.com/method/%s?%saccess_token=%s";

        $params_string = "";
        foreach ($params as $key => $value) {
            $params_string.=$key . '=' . $value;
        }
        if(!empty($params_string)) $params_string.="&";

        $query = sprintf($query, $method, $params_string, $token);
        //echo $query;
        if ($result = @file_get_contents($query)) {
            return json_decode($result, true);
        }
        return FALSE;
    }
	
	function params($params) {
		$pice = array();
		foreach($params as $k=>$v) {
			$pice[] = $k.'='.urlencode($v);
		}
		return implode('&',$pice);
	}
}
?>
